package com.open.api.wechat.config.condition;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @author : zhaoxuan
 * @date : 2021/9/15 0:38
 * @description : WeChatAppCondition
 */
public class WeChatPubCondition implements Condition {

    private static final String EMPTY = "";
    private static final String WECHAT_PUB_APPID = "wechat.pub.appId";
    private static final String WECHAT_PUB_APPSECRET = "wechat.pub.appSecret";

    private static final String WECHAT_PUB_APP_ID = "wechat.pub.app-id";
    private static final String WECHAT_PUB_APP_SECRET = "wechat.pub.app-secret";

    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        Environment environment = conditionContext.getEnvironment();
        String appId = EMPTY;
        if (environment.containsProperty(WECHAT_PUB_APPID)) {
            appId = environment.getProperty(WECHAT_PUB_APPID);
        }
        if (environment.containsProperty(WECHAT_PUB_APP_ID)) {
            appId = environment.getProperty(WECHAT_PUB_APP_ID);
        }
        String appSecret = EMPTY;
        if (environment.containsProperty(WECHAT_PUB_APPSECRET)) {
            appSecret = environment.getProperty(WECHAT_PUB_APPSECRET);
        }
        if (environment.containsProperty(WECHAT_PUB_APP_SECRET)) {
            appSecret = environment.getProperty(WECHAT_PUB_APP_SECRET);
        }
        return StringUtils.isNotBlank(appId) && StringUtils.isNotBlank(appSecret);
    }
}
